package com.yao.club.reposittory;


import com.yao.club.entity.dto.GoodsComment;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.Modifying;

import org.springframework.stereotype.Repository;
import org.springframework.data.domain.Page;

import java.util.List;

@Repository
public interface GoodsCommentRepository extends JpaRepository<GoodsComment, Long> {

    
    List<GoodsComment> findByParentId(Long parentId);
    
    @Modifying
    @Query("UPDATE GoodsComment c SET c.likeNumber = c.likeNumber + 1 WHERE c.goodsCommentId = :commentId")
    void incrementLikeCount(@Param("commentId") Long commentId);

    @Modifying
    @Query("UPDATE GoodsComment c SET c.likeNumber = c.likeNumber - 1 WHERE c.goodsCommentId = :commentId")
    void decrementLikeCount(@Param("commentId") Long commentId);
    
    @Modifying
    @Query("DELETE FROM GoodsComment c WHERE c.goodsCommentId = :commentId OR c.parentId = :commentId")
    void deleteByIdOrParentId(@Param("commentId") Long commentId);

    Page<GoodsComment> findGoodsCommentByGoodsIdAndLevel(Long goodsId, int i,  Pageable pageable);
}
